//
//  ChannnelControllerScrollView.m
//  ChannelCommonComponent
//
//  Created by LiuLian on 14-2-18.
//  Copyright (c) 2014年 LiuLian. All rights reserved.
//

#import "ChannnelControllerScrollView.h"
#import "CLGloble.h"
#import "ChannelComponent.h"
#import "ChannelControllerDataSource.h"
#import "AppDelegate.h"
#import "NSObject+sagittarius.h"

@implementation ChannnelControllerScrollView

- (id)initWithDataSource:(ChannelDataSource *)data
{
  
    CGRect frame = CGRectMake(0, GlobleIndexTableY, GlobleWidth, data.controllerHeight);
    self = [self initWithFrame:frame];
    if (self) {
        // Initialization code
        _child = [NSMutableArray arrayWithCapacity:3];
        for (NSInteger i = 0; i < 3; i++) {
            [_child addObject:[data.controllerClass new]];
        }
        
        [self createChildView];
    }
    return self;
}

- (void)createChildView
{
    for (NSInteger i = 0; i < [_child count]; i++) {
        UIViewController *controller = [_child objectAtIndex:i];
        [controller.view setFrame:CGRectMake(([ChannelComponent currentIndex]+(i-1))*self.frame.size.width, 0, GlobleWidth, self.frame.size.height)];
        
        _channelDelegate = controller;
        if (_channelDelegate && [_channelDelegate respondsToSelector:@selector(updateControllerWithDataSource:)]) {
            ChannelControllerDataSource *dataSource = [ChannelControllerDataSource new];
            dataSource.index = ([ChannelComponent currentIndex]+(i-1));
            switch (i) {
                case 0:
                    dataSource.controllerVisibleState = ControllerVisibleStateBefore;
                    dataSource.isCurrentView = NO;
                    break;
                case 1:
                    dataSource.controllerVisibleState = ControllerVisibleStateCurrent;
                    dataSource.isCurrentView = YES;
                    break;
                case 2:
                    dataSource.controllerVisibleState = ControllerVisibleStateNext;
                    dataSource.isCurrentView = NO;
                    break;
                    
                default:
                    break;
            }
            
            [_channelDelegate updateControllerWithDataSource:dataSource];
            [_channelDelegate updateControllerWithIsCurrent:(i==1)];
        }
        
        [self addSubview:controller.view];
    }
    
    _visibleState = ScrollViewVisibleStateCurrent;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.backgroundColor = [UIColor whiteColor];
        self.delegate = self;
        self.bounces = NO;
        self.pagingEnabled = YES;
        self.showsHorizontalScrollIndicator = NO;
        self.showsVerticalScrollIndicator = NO;
        self.scrollsToTop = NO;
        
        self.contentSize = CGSizeMake([[ChannelComponent onlyTopScrollView].channelList count]*GlobleWidth, self.frame.size.height);
        
        for (NSInteger i = 0; i < CHANNELCOUNT; i++) {
            UIImageView *defaultView = [[UIImageView alloc] initWithFrame:CGRectMake(320*i+(self.frame.size.width-75)/2, (self.frame.size.height-90)/2, 75, 90)];
            [defaultView setBackgroundColor:[UIColor whiteColor]];
          //  [defaultView setImage:[UIImage imageNamed:ICurrentTheme.globleStyle.setBlankButtonImg]];
            [self addSubview:defaultView];
        }
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(updateCSS)
                                                     name:@"NightModeDidChangeNotification"
                                                   object:nil];
        
        _lastCurrentIndex = 0;
        _directionX = 0;
    }
    return self;
}


#pragma mark --UIScrollViewDelegate--
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger redTag = 0;
    NSInteger blackTag = 0;
    
    NSInteger offset_x = (NSInteger)self.contentOffset.x;
    NSInteger width = (NSInteger)GlobleWidth;
    NSInteger remainder = (NSInteger)offset_x%width;
    
    if ([ChannelComponent getDirection]) {
        redTag = (((NSInteger)self.contentOffset.x/self.frame.size.width+100)+1);
        blackTag = ((NSInteger)self.contentOffset.x/self.frame.size.width+100);
    }
    else {
        redTag = ((NSInteger)self.contentOffset.x/self.frame.size.width+100);
        blackTag = ((NSInteger)self.contentOffset.x/self.frame.size.width+100)+1;
        remainder = width-remainder;
    }
    
    if (remainder == 0 || remainder == 320) {
        [ChannelComponent clearTitleWithTag:((NSInteger)self.contentOffset.x/self.frame.size.width+100)];
        return;
    }
    
    [ChannelComponent colorGradientWithWillBlackTag:blackTag WithWillRedTag:redTag WithNumber:remainder];
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    ;
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    ;
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    ;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    [ChannelComponent updateButtonStateWithTag:((NSInteger)self.contentOffset.x/self.frame.size.width+100)];
    [ChannelComponent updateDataSourceWithInCurrent];
}

- (void)willMoveToWindow:(UIWindow *)newWindow
{
//    cactusAppDelegate *delegate = (cactusAppDelegate *)[UIApplication sharedApplication].delegate;
//    if(!delegate.appRootViewController.panpushAnimation.isInitPrepared){
//        [delegate.appRootViewController.panpushAnimation prepareBack];
//    }
}

- (void)updateShadowFrame
{
    NSInteger temp_x = self.contentOffset.x;
    NSInteger temp_i = self.contentOffset.x/320;
    NSInteger temp_n = temp_x%320;
    CGFloat   temp_f = temp_n/320.0;
    if (temp_f == 0) return;
    
  //  UIButton *currentButton = (UIButton *)[[ChannelComponent onlyTopScrollView] viewWithTag:temp_i+100];
//    CGFloat temp_width_c = [currentButton.titleLabel.text length]*20.0;
//    CGFloat temp_width_f = temp_f*[self getWidthWithChannelID:temp_i];
}

- (CGFloat)getFloatWithChannelID:(NSInteger)_channelID
{
    NSInteger temp_a = [[[ChannelComponent onlyTopScrollView].channelList sgrGetStringForIndex:_channelID] length];
    NSInteger temp_b = [[[ChannelComponent onlyTopScrollView].channelList sgrGetStringForIndex:_channelID+1] length];
    return (temp_a+temp_b)*10+14;
}

- (NSInteger)getWidthWithChannelID:(NSInteger)_channelID
{
    
    NSInteger temp_c = [[[ChannelComponent onlyTopScrollView].channelList sgrGetStringForIndex:_channelID] length];
    NSInteger temp_t = [[[ChannelComponent onlyTopScrollView].channelList sgrGetStringForIndex:_channelID+1] length];
    return (temp_t-temp_c)*20;
}

//夜间模式
- (void)updateCSS
{
   // self.backgroundColor = ICurrentTheme.globleStyle.indexTableColor;
    for (UIImageView *imageView in self.subviews) {
//        if ([imageView isKindOfClass:[UIImageView class]]) {
//            [imageView setBackgroundColor:ICurrentTheme.globleStyle.indexTableColor];
//            [imageView setImage:[UIImage imageNamed:ICurrentTheme.globleStyle.setBlankButtonImg]];
//        }
    }
}

@end
